/**
 * 
 */
package org.xlab.baseframework.web.component;

import org.vaadin.peter.buttongroup.ButtonGroup;
import org.xlab.baseframework.domain.DefaultUserDomain;
import org.xlab.baseframework.web.BaseframeworkApplication;
import org.xlab.baseframework.web.data.LoginFormFieldFactory;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;

/**
 * @author makubex
 * 
 */
public class LoginLayout extends VerticalLayout {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 7481261833604749451L;

	private Form loginForm;
	private DefaultUserDomain user;
	private VerticalLayout buttons;

	/**
	 * 默认构造函数
	 */
	public LoginLayout() {
		super();
		this.loginForm = new Form();
		user = new DefaultUserDomain();
		BeanItem<DefaultUserDomain> userItem = new BeanItem<DefaultUserDomain>(user);
		loginForm.setCaption("Ready to start your demo?");
		loginForm.setFormFieldFactory(new LoginFormFieldFactory());
		loginForm.setItemDataSource(userItem);

		Button generate = new Button("Auto Generate",
				new Button.ClickListener() {

					/**
					 * @serialField
					 */
					private static final long serialVersionUID = -3101372666026514997L;

					public void buttonClick(ClickEvent event) {
						user = new DefaultUserDomain();
						user.setUserName("admin");
						user.setRealName("administrator");
						user.setUserCode("admin");
						user.setUserEmail("wuchangmin@wcs-global.com");
						LoginLayout.this.loginForm.setValue(user);
					}
				});
		Button clear = new Button("Clear", new Button.ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -724442550670641309L;

			public void buttonClick(ClickEvent event) {
				user = new DefaultUserDomain();
				LoginLayout.this.loginForm.setValue(user);
			}
		});
		Button start = new Button("Strart Demo!", new Button.ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -1784471995822733548L;

			public void buttonClick(ClickEvent event) {
				LoginLayout.this.loginForm.commit();
				BaseframeworkApplication app = (BaseframeworkApplication) LoginLayout.this
						.getApplication();
				app.setUser(user);
				app.mainWindow.showMain();
			}
		});

		this.buttons = new VerticalLayout();
		buttons.setSpacing(true);
		ButtonGroup group = new ButtonGroup();
		group.addButton(generate);
		group.addButton(clear);
		buttons.addComponent(group);
		buttons.addComponent(start);
		buttons.setComponentAlignment(group, Alignment.MIDDLE_LEFT);
		buttons.setComponentAlignment(start, Alignment.MIDDLE_LEFT);
		buttons.setExpandRatio(group, 1.0f);
		buttons.setExpandRatio(start, 1.0f);

		super.addComponent(loginForm);
		super.addComponent(buttons);
	}
}
